Imports System.Data.SqlClient

Public Class Estudio : Implements IBusquedaFiltrado

    Private idEst As Integer
    Private nombre As String
    Private requisito As String
    Private idTipoEstudio As TipoEstudio
    Private idTipoEst As Integer
    Private precio As Integer
    Public Property tipoEst1() As Integer
        Get
            Return idTipoEst
        End Get
        Set(ByVal value As Integer)
            idTipoEst = value
        End Set
    End Property
    Public Property PrecioEst() As Integer
        Get
            Return precio
        End Get
        Set(ByVal value As Integer)
            precio = value
        End Set
    End Property
    
    Public Sub New()
        idEst = 0
        nombre = ""
        requisito = ""
        idTipoEstudio = Nothing
        precio = 0
    End Sub

    Public Property Identificador() As Integer
        Get
            Return idEst
        End Get
        Set(ByVal value As Integer)
            idEst = value
        End Set
    End Property

    Public Property NombreEstudio() As String
        Get
            Return nombre
        End Get
        Set(ByVal value As String)
            nombre = value
        End Set
    End Property

    Public Property RequisitoEstudio() As String
        Get
            Return Me.requisito
        End Get
        Set(ByVal value As String)
            Me.requisito = value
        End Set
    End Property

    Public Property TipoEstudio() As TipoEstudio
        Get
            Return idTipoEstudio
        End Get
        Set(ByVal value As TipoEstudio)
            idTipoEstudio = value
        End Set
    End Property


    Public Function Agregar(ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Agregar
        Dim comando As SqlCommand = Nothing
        Dim sql As String = ""

        sql = String.Format("INSERT INTO ESTUDIO(IDESTUDIO,NOMBRE,REQUISITO,PRECIO,RANGOVALOR) " + _
                             "VALUES({0},'{1}','{2}',{3},'{4}')", Me.Identificador, Me.nombre, Me.requisito, _
                            Me.idTipoEstudio)
        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If

    End Function

    Public Function Buscar(ByVal id As Integer, ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Buscar
        Dim comando As SqlCommand = Nothing
        Dim sql As String = ""
        Dim lResp As Boolean = False
        Dim rsEstudio As SqlDataReader = Nothing

        sql = String.Format("SELECT * FROM ESTUDIO WHERE IDESTUDIO ={0}", id)
        comando = New SqlCommand(sql, con.ConexionBase)

        rsEstudio = comando.ExecuteReader()
        If rsEstudio.Read() Then
            Me.Identificador = rsEstudio("idEstudio")
            Me.nombre = rsEstudio("nombre").ToString
            Me.requisito = rsEstudio("requisito").ToString
            Me.idTipoEst = rsEstudio("idTipoEstudio")
            Me.PrecioEst = rsEstudio("precio")
            lResp = True

        Else
            lResp = False
        End If
        rsEstudio.Close()
        Return lResp

    End Function

    Public Function Eliminar(ByVal id As Integer, ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Eliminar
        Dim comando As SqlCommand = Nothing
        Dim sql As String = ""

        sql = String.Format("DELETE FROM ESTUDIO WHERE IDESTUDIO ={0}", id)
        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If

    End Function

    Public Function Modificar(ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Modificar
        Dim comando As SqlCommand = Nothing
        Dim sql As String = ""

        sql = String.Format("UPDATE ESTUDIO SET NOMBRE = '{0}',REQUISITO ='{1}', PRECIO ={2},RANGOVALOR='{3}'" + _
                            "  WHERE IDESTUDIO ={4}", Me.nombre, Me.requisito, Me.idTipoEstudio, Me.Identificador)
        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If
    End Function
    Public Function BuscarEst(ByVal idTipoEst As Integer, ByRef con As ConexionBD) As DataSet
        Dim sql As String = ""
        Dim adapter As SqlDataAdapter = Nothing
        Dim datos As DataSet = New DataSet()
        sql = String.Format("SELECT * FROM ESTUDIO WHERE idtipoestudio={0}", idTipoEst)
        adapter = New SqlDataAdapter(sql, con.ConexionBase)
        adapter.Fill(datos, "estudio")

        Return datos
    End Function
    Public Function BuscarId(ByVal DescEst As String, ByRef con As ConexionBD) As Boolean

        Dim comando As SqlCommand
        Dim rs As SqlDataReader
        Dim sql As String
        Dim lResp As Boolean = False

        sql = String.Format("SELECT * FROM ESTUDIO WHERE NOMBRE='{0}'", DescEst)
        comando = New SqlCommand(sql, con.ConexionBase)
        rs = comando.ExecuteReader()
        comando.Dispose()


        If rs.Read Then
            Me.Identificador = rs("idEstudio")
            Me.nombre = rs("nombre").ToString
            Me.requisito = rs("requisito").ToString
            Me.tipoEst1 = rs("IdTipoEstudio")
            Me.PrecioEst = rs("precio")

        Else
            lResp = False
        End If
        rs.Close()
        Return lResp

    End Function
    Public Function BuscarDatos(ByVal id As Integer, ByRef con As ConexionBD) As DataSet

        Dim sql As String = ""
        Dim adapter As SqlDataAdapter = Nothing
        Dim datos As DataSet = New DataSet()

        sql = String.Format("SELECT * FROM ESTUDIO where IdTipoEstudio={0}", id)
        adapter = New SqlDataAdapter(sql, con.ConexionBase)
        adapter.Fill(datos, "estudio")


        Return datos
    End Function
    Public Function BuscarDatosTodo(ByRef con As ConexionBD) As DataSet

        Dim sql As String = ""
        Dim adapter As SqlDataAdapter = Nothing
        Dim datos As DataSet = New DataSet()

        sql = "SELECT * FROM ESTUDIO "
        adapter = New SqlDataAdapter(sql, con.ConexionBase)
        adapter.Fill(datos, "estudio")


        Return datos
    End Function
End Class
